<template>
  <el-dialog
    :title="dialogObj.title"
    append-to-body
    :visible.sync="dialogObj.show"
    :close-on-click-modal="false"
    width="1200px"
  >
    <el-form
      ref="form"
      :model="form"
      label-width="120px"
      :rules="rules"
      :disabled="dialogObj.read"
      v-if="dialogObj.show"
      status-icon
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="客户：" prop="customerId">
            <el-select
              v-model="form.customerId"
              placeholder="请选择"
              style="width:400px;"
              size="mini"
              filterable
              default-first-option
              :disabled="dialogObj.id != ''"
            >
              <el-option
                v-for="(data, index) in customerList"
                :key="index"
                :label="data.customerName"
                :value="data.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态：" prop="status">
            <el-select
              v-model="form.status"
              placeholder="请选择"
              style="width:200px;"
              size="mini"
              :disabled="dialogObj.id != ''"
            >
              <el-option
                v-for="(data, index) in applyStatusList"
                :key="index"
                :label="data.value"
                :value="data.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="案卷号：" prop="orderNumber">
            <el-input
              v-model="form.orderNumber"
              style="width:400px;"
              size="mini"
              maxlength="200"
              :disabled="true"
              placeholder="申请记录添加后自动生成"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="立案日：" prop="registerDate">
            <el-date-picker
              v-model="form.registerDate"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              :disabled="dialogObj.id != ''"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="分类：" prop="applyCategory">
            <el-select
              v-model="form.applyCategory"
              placeholder="请选择"
              style="width:200px;"
              size="mini"
              @change="categoryChanged"
              :disabled="dialogObj.id != ''"
            >
              <el-option
                v-for="(data, index) in applyCategoryList"
                :key="index"
                :label="data.settingCode"
                :value="data.settingValue"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型：" prop="applyType">
            <el-select
              v-model="form.applyType"
              placeholder="请选择"
              style="width:200px;"
              size="mini"
              :disabled="dialogObj.id != ''"
            >
              <el-option
                v-for="(data, index) in applytype"
                :key="index"
                :label="data.settingCode"
                :value="data.settingValue"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="申请名称：" prop="title">
            <el-input
              v-model="form.title"
              style="width:1030px;"
              size="mini"
              maxlength="200"
              placeholder="请输入申请名称"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="简要说明：" prop="description">
          <el-input
            type="textarea"
            v-model="form.description"
            style="width:1030px;"
            size="mini"
            maxlength="1000"
            show-word-limit
            resize="none"
            :autosize="{ minRows: 8 }"
          ></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="代理人：" prop="agent">
            <el-select v-model="form.agentId" style="width:200px;" size="mini">
              <el-option
                v-for="(data, index) in sysuserSelect"
                :key="index"
                :label="data.userName"
                :value="data.userId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="图片：" prop="applyImage">
            <upload-picture
              :imageValue="form.applyImage"
              @handleSuccess="handleSuccess"
            >
            </upload-picture>
            <span style="color:red;font-size:12px">
              提示：只能上传JPG、JPEG、PNG 格式，且不超过5Mb
            </span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="文稿记录：" prop="draft" v-if="dialogObj.id != ''">
        <files-list
          :tableData="tableData1"
          :fileType="1"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-form-item
        label="受理通知书："
        prop="acceptNote"
        v-if="dialogObj.id != ''"
      >
        <files-list
          :tableData="tableData2"
          :fileType="2"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-row v-if="dialogObj.id != ''">
        <el-col :span="12">
          <el-form-item label="申请号：" prop="applyNumber">
            <el-input
              v-model="form.applyNumber"
              style="width:200px;"
              size="mini"
              maxlength="50"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="申请日：" prop="applyDatetime">
            <el-date-picker
              v-model="form.applyDatetime"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              @change="applyDatetimeChanged"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item
        label="缴费通知书："
        prop="payNote"
        v-if="dialogObj.id != ''"
      >
        <files-list
          :tableData="tableData3"
          :fileType="3"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-row v-if="dialogObj.id != ''">
        <el-col :span="12">
          <el-form-item label="缴纳截至时间：" prop="endPayTime">
            <el-date-picker
              v-model="form.endPayTime"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="dialogObj.id != ''">
        <el-col :span="12">
          <el-form-item label="缴纳时间：" prop="applyPayTime">
            <el-date-picker
              v-model="form.applyPayTime"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="申请费(元)：" prop="applyFee">
            <el-input
              v-model="form.applyFee"
              style="width:200px;"
              size="mini"
              maxlength="10"
              oninput="value=value.replace(/[^0-9]/g,'')"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="dialogObj.id != '' && form.applyType === 1">
        <el-col :span="12">
          <el-form-item label="是否提交实质审查请求：" prop="commitExamination">
            <el-select
              v-model="form.commitExamination"
              placeholder="请选择"
              style="width:200px;"
              size="mini"
            >
              <el-option
                v-for="(data, index) in isNotList"
                :key="index"
                :label="data.value"
                :value="data.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否交纳实质审查费：" prop="payExamination">
            <el-select
              v-model="form.payExamination"
              placeholder="请选择"
              style="width:200px;"
              size="mini"
            >
              <el-option
                v-for="(data, index) in isNotList"
                :key="index"
                :label="data.value"
                :value="data.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item
        label="初步审查通知书："
        prop="checkNote"
        v-if="dialogObj.id != '' && form.applyType === 1"
      >
        <files-list
          :tableData="tableData4"
          :fileType="4"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-form-item
        label="发明专利申请公布及进入实质审查通知书："
        prop="examination"
        v-if="dialogObj.id != '' && form.applyType === 1"
      >
        <files-list
          :tableData="tableData5"
          :fileType="5"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-form-item
        label="补正通知书："
        prop="correctNote"
        v-if="dialogObj.id != ''"
      >
        <files-list
          :tableData="tableData6"
          :fileType="6"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-row v-if="dialogObj.id != ''">
        <el-col :span="12">
          <el-form-item label="下发补正时间：" prop="issueCorrectTime">
            <el-date-picker
              v-model="form.issueCorrectTime"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              @change="issueCorrectTimeChanged"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="补正截止时间：" prop="endCorrectTime">
            <el-date-picker
              v-model="form.endCorrectTime"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item
        label="答复补正文件："
        prop="correctNote"
        v-if="dialogObj.id != ''"
      >
        <files-list
          :tableData="tableData61"
          :fileType="61"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-row v-if="dialogObj.id != ''">
        <el-col :span="12">
          <el-form-item label="答复补正时间：" prop="answerCorrectTime">
            <el-date-picker
              v-model="form.answerCorrectTime"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="答复补正结果文件：" prop="answerCorrectFile">
            <upload-file
              :imageValue="form.answerCorrectFile"
              :isNotlist="true"
              @handlefileSuccess="answerCorrectFileSuccess"
            >
            </upload-file>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item
        label="审查意见："
        prop="examinationOpinion"
        v-if="dialogObj.id != ''"
      >
        <files-list
          :tableData="tableData7"
          :fileType="7"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-row v-if="dialogObj.id != ''">
        <el-col :span="12">
          <el-form-item label="下发审查意见时间：" prop="issueExaminationTime">
            <el-date-picker
              v-model="form.issueExaminationTime"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              @change="issueExaminationTimeChanged"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="审查意见截止时间：" prop="endExaminationTime">
            <el-date-picker
              v-model="form.endExaminationTime"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item
        label="答复审查意见文件："
        prop="correctNote"
        v-if="dialogObj.id != ''"
      >
        <files-list
          :tableData="tableData71"
          :fileType="71"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-row v-if="dialogObj.id != ''">
        <el-col :span="12">
          <el-form-item
            label="答复审查意见时间："
            prop="answerExaminationTime"
          >
            <el-date-picker
              v-model="form.answerExaminationTime"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="答复审查意见结果文件："
            prop="answerExaminationFile"
          >
            <upload-file
              :imageValue="form.answerExaminationFile"
              :isNotlist="true"
              @handlefileSuccess="answerExaminationFileSuccess"
            >
            </upload-file>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item
        label="授权通知书："
        prop="authorizeNote"
        v-if="dialogObj.id != ''"
      >
        <files-list
          :tableData="tableData8"
          :fileType="8"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-row v-if="dialogObj.id != ''">
        <el-col :span="12">
          <el-form-item label="授权日：" prop="authorizeDatetime">
            <el-date-picker
              v-model="form.authorizeDatetime"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              @change="authorizeDatetimeChange"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.applyCategory == 1">
          <el-form-item label="交纳登记费截止时间：" prop="endRegistrationTime">
            <el-date-picker
              v-model="form.endRegistrationTime"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.applyCategory != 1">
          <el-form-item label="续展截至时间：" prop="endRegistrationTime">
            <el-date-picker
              v-model="form.endRegistrationTime"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="dialogObj.id != '' && form.applyCategory == 1">
        <el-col :span="12">
          <el-form-item label="是否缴纳登记费：" prop="payRegistrationFee">
            <el-select
              v-model="form.payRegistrationFee"
              placeholder="请选择"
              style="width:200px;"
              size="mini"
            >
              <el-option
                v-for="(data, index) in isNotList"
                :key="index"
                :label="data.value"
                :value="data.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="费减情况：" prop="feeDecrease">
            <el-select
              v-model="form.feeDecrease"
              placeholder="请选择"
              style="width:200px;"
              size="mini"
            >
              <el-option
                v-for="(data, index) in feeDecreaseList"
                :key="index"
                :label="data.value"
                :value="data.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="dialogObj.id != '' && form.applyCategory == 1">
        <el-col :span="12">
          <el-form-item label="已缴费年费：" prop="annualFeePay">
            <el-select
              v-model="form.annualFeePay"
              placeholder="请选择"
              style="width:200px;"
              size="mini"
            >
              <el-option
                v-for="(data, index) in annualFeeList"
                :key="index"
                :label="data.value"
                :value="data.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="登记费(元)：" prop="registrationFee">
            <el-input
              v-model="form.registrationFee"
              style="width:200px;"
              size="mini"
              maxlength="10"
              oninput="value=value.replace(/[^0-9]/g,'')"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="证书：" prop="certificate" v-if="dialogObj.id != ''">
        <files-list
          :tableData="tableData9"
          :fileType="9"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-row v-if="dialogObj.id != ''">
        <el-col :span="12">
          <el-form-item label="公开号：" prop="publicNumber">
            <el-input
              v-model="form.publicNumber"
              style="width:200px;"
              size="mini"
              maxlength="50"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公开日：" prop="publicDatetime">
            <el-date-picker
              v-model="form.publicDatetime"
              style="width:200px;"
              size="mini"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              @change="applyDatetimeChanged"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="驳回：" prop="rejectNote" v-if="dialogObj.id != ''">
        <files-list
          :tableData="tableData10"
          :fileType="10"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-form-item
        label="官费收据："
        prop="rejectNote"
        v-if="dialogObj.id != ''"
      >
        <files-list
          :tableData="tableData11"
          :fileType="11"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-form-item
        label="合同、发票及其他文件："
        prop="contract"
        v-if="dialogObj.id != ''"
      >
        <files-list
          :tableData="tableData12"
          :fileType="12"
          :applyId="dialogObj.id"
          @handleTableDate="handleTableDate"
        >
        </files-list>
      </el-form-item>
      <el-row v-if="dialogObj.id != ''">
        <el-col :span="12">
          <el-form-item label="合同编号：" prop="contractNumber">
            <el-input
              v-model="form.contractNumber"
              style="width:200px;"
              size="mini"
              maxlength="200"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否到账：" prop="accountTransfer">
            <el-select
              v-model="form.accountTransfer"
              placeholder="请选择"
              style="width:200px;"
              size="mini"
            >
              <el-option
                v-for="(data, index) in isNotList"
                :key="index"
                :label="data.value"
                :value="data.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer" v-if="!dialogObj.read">
      <el-button type="primary" @click="sub" size="mini">保 存</el-button>
      <el-button @click="dialogObj.show = false" size="mini">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import uploadPicture from "@c/uploadPicture";
import filesList from "./filesList";
import { _debounce } from "@u/debounce";
import { validatePatentNumber, validateEmpty } from "@u/validate";
import uploadFile from "@c/uploadFile";
import {
  PATENTTYPELIST,
  ISNOTLIST,
  APPLYSTATUSLIST,
  FEEDECREASELIST,
  ANNUALFEELIST,
  APPLYFILELIST
} from "@u/wordbook";
import {
  AddApplyManageApi,
  updateApplyManageApi,
  getCustomerListApi,
  getApplyManageApi,
  pushWxTplMsgApi,
  submitApplyFileListApi,
  getSysuserSelectApi,
  applyCategoryListApi
} from "@s/api";
export default {
  name: "dialogCom",
  props: ["dialogObj"],
  components: {
    uploadPicture,
    filesList,
    uploadFile
  },
  data() {
    return {
      agentStatus: true,
      isUpdate: false,
      // loginName: "",
      sysuserSelect: [], //代理人
      tableData1: [],
      tableData2: [],
      tableData3: [],
      tableData4: [],
      tableData5: [],
      tableData6: [],
      tableData61: [],
      tableData7: [],
      tableData71: [],
      tableData8: [],
      tableData9: [],
      tableData10: [],
      tableData11: [],
      tableData12: [],
      form: {
        id: "",
        agentId: "",
        customerId: "",
        orderNumber: "",
        applyCategory: "",
        applyType: "",
        registerDate: "",
        title: "",
        applyNumber: "",
        applyDatetime: "",
        description: "",
        applyImage: "",
        draftHave: "",
        acceptNoteHave: "",
        payNoteHave: "",
        endPayTime: "",
        checkNoteHave: "",
        examinationHave: "",
        correctNoteHave: "",
        examinationOpinionHave: "",
        authorizeNoteHave: "",
        certificateHave: "",
        rejectNoteHave: "",
        receiptHave: "",
        contractHave: "",
        applyPayTime: "",
        applyFee: "",
        commitExamination: "",
        payExamination: "",
        contractNumber: "",
        accountTransfer: "",
        issueCorrectTime: "",
        endCorrectTime: "",
        answerCorrectTime: "",
        issueExaminationTime: "",
        endExaminationTime: "",
        answerExaminationTime: "",
        payRegistrationFee: "",
        endRegistrationTime: "",
        registrationFee: "",
        authorizeDatetime: "",
        answerCorrectFile: "",
        answerExaminationFile: "",
        publicNumber: "",
        publicDatetime: "",
        agent: "",
        annualFeePay: "",
        feeDecrease: "",
        status: "",
        filesInputBeanList: [],
        tableData1: [],
        tableData2: [],
        tableData3: [],
        tableData4: [],
        tableData5: [],
        tableData6: [],
        tableData61: [],
        tableData7: [],
        tableData71: [],
        tableData8: [],
        tableData9: [],
        tableData10: [],
        tableData11: [],
        tableData12: []
      },
      patentTypeList: PATENTTYPELIST,
      isNotList: ISNOTLIST,
      applyStatusList: APPLYSTATUSLIST,
      applyCategoryList: [],
      feeDecreaseList: FEEDECREASELIST,
      annualFeeList: ANNUALFEELIST,
      applyFileList: APPLYFILELIST,
      applytype: [],
      customerList: [],
      rules: {
        customerId: [
          { required: true, trigger: "blur", message: "请选择客户名称" },
          { validator: validateEmpty }
        ],
        registerDate: [
          { required: true, trigger: "blur", message: "请输入立案日" },
          { validator: validateEmpty }
        ],
        title: [
          { required: true, trigger: "blur", message: "请输入申请名称" },
          { validator: validateEmpty }
        ],
        applyCategory: [
          { required: true, trigger: "blur", message: "请输入区分" },
          { validator: validateEmpty }
        ],
        applyType: [
          { required: true, trigger: "blur", message: "请输入类型" },
          { validator: validateEmpty }
        ],
        agentId: [
          { required: true, trigger: "blur", message: "请输入代理人" },
          { validator: validateEmpty }
        ],
        status: [
          { required: true, trigger: "blur", message: "请输入状态" },
          { validator: validateEmpty }
        ],
        applyNumber: [{ validator: validatePatentNumber }],
        contractNumber: [
          { required: true, trigger: "blur", message: "请输入合同编号" },
          { validator: validateEmpty }
        ],
        accountTransfer: [
          { required: true, trigger: "blur", message: "请选择是否到账" },
          { validator: validateEmpty }
        ]
      }
    };
  },
  methods: {
    selectBlur(e) {
      this.form.customerId = e.target.value;
    },
    // 客户信息
    getCustomerSelect() {
      getCustomerListApi().then(res => {
        this.customerList = res;
      });
    },
    categoryChanged(value) {
      this.applyCategoryList.filter(item => {
        item.settingValue = Number(item.settingValue)
        if (item.settingValue === value) {
           item.sons.map(i => {
             i.settingValue = Number(i.settingValue)
           })
           this.applytype = item.sons;
        }
      })
      console.log(this.applyCategoryList, 'this.applyCategoryList')
    },
    applyDatetimeChanged(value) {
      let endPayTime = this.dayjs(value)
        .add(55, "day")
        .format("YYYY-MM-DD");
      this.form.endPayTime = endPayTime;
    },
    issueCorrectTimeChanged(value) {
      let endCorrectTime = this.dayjs(value)
        .add(55, "day")
        .format("YYYY-MM-DD");
      this.form.endCorrectTime = endCorrectTime;
    },
    issueExaminationTimeChanged(value) {
      let endExaminationTime = this.dayjs(value)
        .add(55, "day")
        .format("YYYY-MM-DD");
      this.form.endExaminationTime = endExaminationTime;
    },
    authorizeDatetimeChange(value) {
      let endRegistrationTime;
      if (this.form.applyCategory == 1) {
        endRegistrationTime = this.dayjs(value)
          .add(55, "day")
          .format("YYYY-MM-DD");
      } else {
        endRegistrationTime = this.dayjs(value)
          .add(9, "year")
          .add(6, "month")
          .format("YYYY-MM-DD");
      }
      this.form.endRegistrationTime = endRegistrationTime;
    },
    answerCorrectFileSuccess(val) {
      this.form.answerCorrectFile = val;
      this.form.answerCorrectTime = this.dayjs().format("YYYY-MM-DD");
    },
    answerExaminationFileSuccess(val) {
      this.form.answerExaminationFile = val;
      this.form.answerExaminationTime = this.dayjs().format("YYYY-MM-DD");
    },
    // 代理人
    getSysuserSelect(val) {
      getSysuserSelectApi({ name: val }).then((res) => {
        this.sysuserSelect = res
      });
    },
    applyCategoryListHandle() {
      applyCategoryListApi().then(res => {
        this.applyCategoryList = res
      })
    },

    initDialog() {
      this.getSysuserSelect("代理人");
      this.applyCategoryListHandle()
      if (this.dialogObj.id) {
        let json = { applyId: this.dialogObj.id };
        getApplyManageApi(json).then(res => {
          this.form.id = this.dialogObj.id;
          this.form.customerId = res.customerId;
          this.form.agentId = res.agentId;
          this.form.orderNumber = res.orderNumber;
          this.form.applyCategory = res.applyCategory;
          this.form.applyType = res.applyType;
          this.categoryChanged(res.applyCategory);
          this.form.registerDate = res.registerDate;
          this.form.title = res.title;
          this.form.applyImage = res.applyImage;
          this.form.applyNumber = res.applyNumber;
          this.form.applyDatetime = res.applyDatetime;
          this.form.description = res.description;
          this.form.draftHave = res.draftHave;
          this.form.acceptNoteHave = res.acceptNoteHave;
          this.form.payNoteHave = res.payNoteHave;
          this.form.endPayTime = res.endPayTime;
          this.form.checkNoteHave = res.checkNoteHave;
          this.form.examinationHave = res.examinationHave;
          this.form.correctNoteHave = res.correctNoteHave;
          this.form.examinationOpinionHave = res.examinationOpinionHave;
          this.form.authorizeNoteHave = res.authorizeNoteHave;
          this.form.certificateHave = res.certificateHave;
          this.form.rejectNoteHave = res.rejectNoteHave;
          this.form.receiptHave = res.receiptHave;
          this.form.contractHave = res.contractHave;
          this.form.applyPayTime = res.applyPayTime;
          this.form.applyFee = res.applyFee;
          this.form.commitExamination = res.commitExamination;
          this.form.payExamination = res.payExamination;
          this.form.contractNumber = res.contractNumber;
          this.form.accountTransfer = res.accountTransfer;
          this.form.issueCorrectTime = res.issueCorrectTime;
          this.form.endCorrectTime = res.endCorrectTime;
          this.form.answerCorrectTime = res.answerCorrectTime;
          this.form.issueExaminationTime = res.issueExaminationTime;
          this.form.endExaminationTime = res.endExaminationTime;
          this.form.answerExaminationTime = res.answerExaminationTime;
          this.form.payRegistrationFee = res.payRegistrationFee;
          this.form.endRegistrationTime = res.endRegistrationTime;
          this.form.registrationFee = res.registrationFee;
          this.form.annualFeePay = res.annualFeePay;
          this.form.feeDecrease = res.feeDecrease;
          this.form.answerCorrectFile = res.answerCorrectFile;
          this.form.answerExaminationFile = res.answerExaminationFile;
          this.form.publicNumber = res.publicNumber;
          this.form.publicDatetime = res.publicDatetime;
          this.form.agent = res.agent;
          this.form.authorizeDatetime = res.authorizeDatetime;
          this.form.status = res.status;
          if (res.filesOutputBeanList1) {
            this.form.tableData1 = res.filesOutputBeanList1;
            this.tableData1 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList1)
            );
            console.log(res.filesOutputBeanList1, 'res.filesOutputBeanList1')
          } else {
            this.form.tableData1 = [];
            this.tableData1 = [];
          }
          if (res.filesOutputBeanList2) {
            this.form.tableData2 = res.filesOutputBeanList2;
            this.tableData2 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList2)
            );
          } else {
            this.form.tableData2 = [];
            this.tableData2 = [];
          }
          if (res.filesOutputBeanList3) {
            this.form.tableData3 = res.filesOutputBeanList3;
            this.tableData3 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList3)
            );
          } else {
            this.form.tableData3 = [];
            this.tableData3 = [];
          }
          if (res.filesOutputBeanList4) {
            this.form.tableData4 = res.filesOutputBeanList4;
            this.tableData4 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList4)
            );
          } else {
            this.form.tableData4 = [];
            this.tableData4 = [];
          }
          if (res.filesOutputBeanList5) {
            this.form.tableData5 = res.filesOutputBeanList5;
            this.tableData5 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList5)
            );
          } else {
            this.form.tableData5 = [];
            this.tableData5 = [];
          }
          if (res.filesOutputBeanList6) {
            this.form.tableData6 = res.filesOutputBeanList6;
            this.tableData6 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList6)
            );
          } else {
            this.form.tableData6 = [];
            this.tableData6 = [];
          }
          if (res.filesOutputBeanList61) {
            this.form.tableData61 = res.filesOutputBeanList61;
            this.tableData61 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList61)
            );
          } else {
            this.form.tableData61 = [];
            this.tableData61 = [];
          }
          if (res.filesOutputBeanList7) {
            this.form.tableData7 = res.filesOutputBeanList7;
            this.tableData7 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList7)
            );
          } else {
            this.form.tableData7 = [];
            this.tableData7 = [];
          }
          if (res.filesOutputBeanList71) {
            this.form.tableData71 = res.filesOutputBeanList71;
            this.tableData71 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList71)
            );
          } else {
            this.form.tableData71 = [];
            this.tableData71 = [];
          }
          if (res.filesOutputBeanList8) {
            this.form.tableData8 = res.filesOutputBeanList8;
            this.tableData8 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList8)
            );
          } else {
            this.form.tableData8 = [];
            this.tableData8 = [];
          }
          if (res.filesOutputBeanList9) {
            this.form.tableData9 = res.filesOutputBeanList9;
            this.tableData9 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList9)
            );
          } else {
            this.form.tableData9 = [];
            this.tableData9 = [];
          }
          if (res.filesOutputBeanList10) {
            this.form.tableData10 = res.filesOutputBeanList10;
            this.tableData10 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList10)
            );
          } else {
            this.form.tableData10 = [];
            this.tableData10 = [];
          }

          if (res.filesOutputBeanList11) {
            this.form.tableData11 = res.filesOutputBeanList11;
            this.tableData11 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList11)
            );
          } else {
            this.form.tableData11 = [];
            this.tableData11 = [];
          }
          if (res.filesOutputBeanList12) {
            this.form.tableData12 = res.filesOutputBeanList12;
            this.tableData12 = JSON.parse(
              JSON.stringify(res.filesOutputBeanList12)
            );
          } else {
            this.form.tableData12 = [];
            this.tableData12 = [];
          }
        });
        this.isUpdate = true;
      } else {
        this.form.id = "";
        this.form.customerId = "";
        this.form.orderNumber = "";
        this.form.applyCategory = "";
        this.form.applyType = "";
        this.form.registerDate = "";
        this.form.title = "";
        this.form.applyImage = "";
        this.form.applyNumber = "";
        this.form.applyDatetime = "";
        this.form.description = "";
        this.form.draftHave = "";
        this.form.agentId = "";
        this.form.acceptNoteHave = "";
        this.form.payNoteHave = "";
        this.form.endPayTime = "";
        this.form.checkNoteHave = "";
        this.form.examinationHave = "";
        this.form.correctNoteHave = "";
        this.form.examinationOpinionHave = "";
        this.form.authorizeNoteHave = "";
        this.form.certificateHave = "";
        this.form.rejectNoteHave = "";
        this.form.receiptHave = "";
        this.form.contractHave = "";
        this.form.applyPayTime = "";
        this.form.applyFee = "";
        this.form.commitExamination = "";
        this.form.payExamination = "";
        this.form.contractNumber = "";
        this.form.accountTransfer = "";
        this.form.issueCorrectTime = "";
        this.form.endCorrectTime = "";
        this.form.answerCorrectTime = "";
        this.form.issueExaminationTime = "";
        this.form.endExaminationTime = "";
        this.form.answerExaminationTime = "";
        this.form.payRegistrationFee = "";
        this.form.endRegistrationTime = "";
        this.form.registrationFee = "";
        this.form.annualFeePay = "";
        this.form.feeDecrease = "";
        this.form.answerCorrectFile = "";
        this.form.answerExaminationFile = "";
        this.form.publicNumber = "";
        this.form.publicDatetime = "";
        this.form.agent = "";
        this.form.authorizeDatetime = "";
        this.form.status = "";
        this.form.createUser = "";
        this.form.filesInputBeanList = [];
        this.isUpdate = false;
      }
    },
    sub: _debounce(function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.dialogObj.id) {
            this.updateSub();
          } else {
            this.addSub();
          }
        }
      });
    }),
    updateSub() {
      // 上传受理通知书后校验申请号和入申请时间
      if (this.tableData2.length > 0) {
        if (
          this.form.applyNumber == "" ||
          this.form.applyNumber == null ||
          this.form.applyDatetime == "" ||
          this.form.applyDatetime == null
        ) {
          this.$message({
            type: "warning",
            message: "请填写申请号和申请日期后再保存！"
          });
          return;
        }
      }
      // 上传补正通知书后校验下发补正通知书时间和补正截止时间
      if (this.tableData6.length > 0) {
        if (
          this.form.issueCorrectTime == "" ||
          this.form.issueCorrectTime == null ||
          this.form.endCorrectTime == "" ||
          this.form.endCorrectTime == null
        ) {
          this.$message({
            type: "warning",
            message: "请填写下发补正时间和补正截止时间后再保存！"
          });
          return;
        }
      }
      // 上传审查意见后校验下发审查意见时间和审查意见截止时间
      if (this.tableData7.length > 0) {
        if (
          this.form.issueExaminationTime == "" ||
          this.form.issueExaminationTime == null ||
          this.form.endExaminationTime == "" ||
          this.form.endExaminationTime == null
        ) {
          this.$message({
            type: "warning",
            message: "请填写下发审查意见时间和审查意见截止时间后再保存！"
          });
          return;
        }
      }
      // 上传授权通知书后校验授权日期和缴费截至日期
      if (this.tableData8.length > 0) {
        if (
          this.form.authorizeDatetime == "" ||
          this.form.authorizeDatetime == null ||
          this.form.endRegistrationTime == "" ||
          this.form.endRegistrationTime == null
        ) {
          this.$message({
            type: "warning",
            message: "请填写授权日期和缴纳登记费截止时间后再保存！"
          });
          return;
        }
      }
      // 上传文件信息校验
      if (!this.checkFileInfo(this.tableData1, 1)) {
        return;
      }
      if (!this.checkFileInfo(this.tableData2, 2)) {
        return;
      }
      if (!this.checkFileInfo(this.tableData3, 3)) {
        return;
      }
      if (!this.checkFileInfo(this.tableData4, 4)) {
        return;
      }
      if (!this.checkFileInfo(this.tableData5, 5)) {
        return;
      }
      if (!this.checkFileInfo(this.tableData6, 6)) {
        return;
      }
      if (!this.checkFileInfo(this.tableData61, 61)) {
        return;
      }
      if (!this.checkFileInfo(this.tableData7, 7)) {
        return;
      }
      if (!this.checkFileInfo(this.tableData71, 71)) {
        return;
      }
      if (!this.checkFileInfo(this.tableData8, 8)) {
        return;
      }
      if (!this.checkFileInfo(this.tableData9, 9)) {
        return;
      }
      if (!this.checkFileInfo(this.tableData10, 10)) {
        return;
      }
      if (!this.checkFileInfo(this.tableData11, 11)) {
        return;
      }
      if (!this.checkFileInfo(this.tableData12, 12)) {
        return;
      }
      // 是否状态变更推送消息
      let newStatus = this.getStatus();
      if(this.form.tableData6.length !== this.tableData6.length) {
         this.form.answerCorrectFile = "";
         this.form.answerCorrectTime = "";
         newStatus = 5
      }
      if(this.form.tableData7.length !== this.tableData7.length) {
        this.form.answerExaminationFile = "";
        this.form.answerExaminationTime = "";
        newStatus = 5
      }
      this.form.status = newStatus;
      let isPushMes = this.form.status == newStatus ? false : true;
      let json = JSON.parse(JSON.stringify(this.form));
     
      updateApplyManageApi(json).then(() => {
        // 判断上传文件变更，进行保存
        this.saveFileList(this.form.tableData1, this.tableData1, 1);
        this.saveFileList(this.form.tableData2, this.tableData2, 2);
        this.saveFileList(this.form.tableData3, this.tableData3, 3);
        this.saveFileList(this.form.tableData4, this.tableData4, 4);
        this.saveFileList(this.form.tableData5, this.tableData5, 5);
        this.saveFileList(this.form.tableData6, this.tableData6, 6);
        this.saveFileList(this.form.tableData61, this.tableData61, 61);
        this.saveFileList(this.form.tableData7, this.tableData7, 7);
        this.saveFileList(this.form.tableData71, this.tableData71, 71);
        this.saveFileList(this.form.tableData8, this.tableData8, 8);
        this.saveFileList(this.form.tableData9, this.tableData9, 9);
        this.saveFileList(this.form.tableData10, this.tableData10, 10);
        this.saveFileList(this.form.tableData11, this.tableData11, 11);
        this.saveFileList(this.form.tableData12, this.tableData12, 12);
        if (isPushMes) {
          // 状态变更进行微信推送
          let pushjson = {
            applyId: this.form.id
          };
          pushWxTplMsgApi(pushjson).then();
        }
        this.dialogObj.show = false;
        this.$emit("reflash");
        this.$message({
          type: "success",
          message: "修改成功！"
        });
      });
    },
    checkFileInfo(arr_new, type) {
      var fileName = this.applyFileList.find(v => v.id === type).value;
      // 文件校验
      for (var i = 0; i < arr_new.length; i++) {
        if (arr_new[i].fileName == "") {
          this.$message({
            type: "warning",
            message: fileName + " 请输入文件名称后再保存！"
          });
          return false;
        }
        if (arr_new[i].filePath == "") {
          this.$message({
            type: "warning",
            message: fileName + " 请上传文件后再保存！"
          });
          return false;
        }
      }
      return true;
    },
    saveFileList(arr_org, arr_new, type) {
      if (arr_org.length == arr_new.length) {
        let isSave = false;
        // 个数一样
        for (var i = 0; i < arr_org.length; i++) {
          if (
            arr_org[i].createTime != arr_new[i].createTime ||
            arr_org[i].createUser != arr_new[i].createUser ||
            arr_org[i].fileDesc != arr_new[i].fileDesc ||
            arr_org[i].fileName != arr_new[i].fileName ||
            arr_org[i].filePath != arr_new[i].filePath ||
            arr_org[i].fileType != arr_new[i].fileType ||
            arr_org[i].id != arr_new[i].id ||
            arr_org[i].patentApplyId != arr_new[i].patentApplyId ||
            arr_org[i].updateTime != arr_new[i].updateTime ||
            arr_org[i].updateUser != arr_new[i].updateUser
          ) {
            // 有不同文件存在
            isSave = true;
            break;
          }
        }
        if (isSave) {
          // 保存文件
          this.submitFileList(arr_new, type);
        }
      } else {
        // 个数不一样 保存文件
        this.submitFileList(arr_new, type);
      }
    },
    submitFileList(arr_new, type) {
      let json = {
        applyId: this.form.id,
        fileType: type,
        applyFilesList: arr_new
      };
      submitApplyFileListApi(json).then();
    },
    addSub() {
      let json = JSON.parse(JSON.stringify(this.form));
      AddApplyManageApi(json).then(() => {
        this.dialogObj.show = false;
        this.$emit("reflash");
        this.$message({
          type: "success",
          message: "添加成功！"
        });
      });
    },
    getStatus() {
      // 上传驳回通知书为驳回
      if (this.tableData10 != null && this.tableData10.length > 0) {
        return 9;
      }
      // 上传专利证书为已下证
      if (this.tableData9 != null && this.tableData9.length > 0) {
        return 7;
      }
      // 已交纳登记费为已授权已缴费
      if (this.form.payRegistrationFee == 1) {
        return 61;
      }
      // 上传授权通知书为已授权未缴费
      if (this.tableData8 != null && this.tableData8.length > 0) {
        if (this.form.applyCategory == 1) {
          // 专利申请，为已授权未缴费
          return 6;
        } else {
          // 商标或版权申请，为已授权已缴费
          return 61;
        }
      }
      // 填写答复补正审查意见时间为答复补正/审查意见
      if (
        this.form.answerExaminationTime != null &&
        this.form.answerExaminationTime != "" &&
        this.form.answerExaminationFile != null &&
        this.form.answerExaminationFile != ""
      ) {
        return 51;
      }
      // 上传审查意见为下发补正/审查意见
      if (this.tableData7 != null && this.tableData7.length > 0) {
        return 5;
      }
      // 填写答复补正时间为答复补正/审查意见
      if (
        this.form.answerCorrectTime != null &&
        this.form.answerCorrectTime != "" &&
        this.form.answerCorrectFile != null &&
        this.form.answerCorrectFile != ""
      ) {
        return 51;
      }
      // 上传补正通知书为下发补正/审查意见
      if (this.tableData6 != null && this.tableData6.length > 0) {
        return 5;
      }
      // 上传发明专利申请公布及进入实质审查通知书为进入实质审查
      if (this.tableData5 != null && this.tableData5.length > 0) {
        return 4;
      }
      // 上传初步审查通知书为进入实质审查
      if (this.tableData4 != null && this.tableData4.length > 0) {
        return 4;
      }
      // 有申请费缴费时间为已申请已缴费
      if (this.form.applyPayTime != null && this.form.applyPayTime != "") {
        return 3;
      }
      // 上传受理通知书为已申请未缴费
      if (this.tableData2 != null && this.tableData2.length > 0) {
        return 2;
      }
      // 上传文稿记录为专利撰写中
      if (this.tableData1 != null && this.tableData1.length > 0) {
        return 0;
      }
      // 默认专利撰写中
      return 0;
    },
    selectIsAgentOption(val) {
      if (val == "0") {
        this.agentStatus = true;
      } else {
        this.agentStatus = false;
      }
    },
    handleTableDate() {
      // console.log(this.tableData6.length, 'tableData1')
      // console.log(this.tableData7.length, 'tableData7')
    },
    // 图片上传
    handleSuccess(val) {
      this.form.applyImage = val;
    }
  },
  watch: {
    "dialogObj.show"(val) {
      if (val) {
        this.getCustomerSelect();
        this.initDialog();
      }
    }
  }
};
</script>
<style scoped lang="scss"></style>
